package cn.hbwe.service.impl;

import cn.hbwe.mapper.PatientMapper;
import cn.hbwe.pojo.entity.Booking;
import cn.hbwe.mapper.BookingMapper;
import cn.hbwe.pojo.entity.Doctor;
import cn.hbwe.pojo.entity.Patient;
import cn.hbwe.pojo.entity.Schedule;
import cn.hbwe.pojo.vo.BookingVo;
import cn.hbwe.service.IBookingService;
import cn.hbwe.service.IDoctorService;
import cn.hbwe.service.IPatientService;
import cn.hbwe.service.IScheduleService;
import cn.hbwe.util.PageVO;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@Service
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking> implements IBookingService {

    @Autowired
    private IPatientService patientService;
    @Autowired
    private IScheduleService scheduleService;

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private BookingMapper bookingMapper;



    /**
     * 病人预约医生
     *
     * @param booking
     * @return
     */
    @Override
    @Transactional
    public void booking(Booking booking) {
        Integer doctorId = booking.getDoctorId();
        LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(doctorId != null, Schedule::getDoctorId, doctorId);
        booking.setBookingNow(LocalDateTime.now());
        this.save(booking);
    }

    /**
     * 根据医生id查看预约信息
     * @param id
     * @return
     */
    @Override
    public List<BookingVo> getBookingByDoctorId(Integer id) {
        return bookingMapper.getBookingByDoctorId(id);
    }




    @Override
    public PageVO<BookingVo> getPage(Long currentPage, Long pageSize, Integer patientId) {
        Page<Booking> page = Page.of(currentPage, pageSize);

        LambdaQueryWrapper<Booking> lqw = new LambdaQueryWrapper<>();
        lqw.eq(patientId != null, Booking::getPatientId, patientId);
        lqw.orderByDesc(Booking::getBookingTime);
        Page<Booking> bookingPage = page(page,lqw);
        long total = bookingPage.getTotal();
        long current = bookingPage.getCurrent();
        List<Booking> bookings = bookingPage.getRecords();

        List<BookingVo> bookingVos = BeanUtil.copyToList(bookings, BookingVo.class);

        for (Booking booking : bookings) {
            Integer doctorId = booking.getDoctorId();
            Doctor doctor = doctorService.getById(doctorId);

            Integer patientIId = booking.getPatientId();
            Patient patient =patientService.getById(patientIId);


            for (BookingVo bookingVo : bookingVos) {
                if (patientIId == bookingVo.getPatientId()) {
                    bookingVo.setPatientName(patient.getPatientName());
                }

                if (doctorId == bookingVo.getDoctorId()) {
                    bookingVo.setPrice(doctor.getPrice());
                    bookingVo.setDoctorName(doctor.getDoctorName());
                }
            }

        }
        return PageVO.of(total, current, bookingVos);
    }

//-------------y-----------

    //更改就诊状态
    public void updateBookingStateTwo(int bookingId){
        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("booking_id",bookingId);
        Booking booking=bookingMapper.selectById(bookingId);
        booking.setBookingState(2);
        bookingMapper.update(booking,queryWrapper);
    }
    public void updateBookingStateThree(int bookingId){
        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("booking_id",bookingId);
        Booking booking=bookingMapper.selectById(bookingId);
        booking.setBookingState(3);
        bookingMapper.update(booking,queryWrapper);
    }


    //通过医生id查询预约表

    @Override
    public PageVO<BookingVo> getPageByDoctorId(Long currentPage, Long pageSize, Integer doctorId) {
        Page<Booking> page = Page.of(currentPage, pageSize);

        LambdaQueryWrapper<Booking> lqw = new LambdaQueryWrapper<>();
        lqw.eq(doctorId != null, Booking::getDoctorId, doctorId);

        LocalDateTime now = LocalDateTime.now(); // 获取当前时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 定义格式
        String formattedNow = now.format(formatter); // 格式化时间

        lqw.like(Booking::getBookingTime,formattedNow);
        Page<Booking> bookingPage = page(page,lqw);
        long total = bookingPage.getTotal();
        long current = bookingPage.getCurrent();
        List<Booking> bookings = bookingPage.getRecords();

        List<BookingVo> bookingVos = BeanUtil.copyToList(bookings, BookingVo.class);
        for (Booking booking : bookings) {
            Integer patientId = booking.getPatientId();
            Patient patient = patientService.getById(patientId);
            Doctor doctor = doctorService.getById(doctorId);
            for (BookingVo bookingVo : bookingVos) {
                if (patientId == bookingVo.getPatientId()) {
                    bookingVo.setPatientName(patient.getPatientName());
                }
                if (doctorId == bookingVo.getDoctorId()) {
                    bookingVo.setDoctorName(doctor.getDoctorName());
                }
            }
        }
        return PageVO.of(total, current, bookingVos);
    }



}
